package com.nulldev.util.crypto;

import com.nulldev.util.string.StringsUtil;
import com.nulldev.util.string.UnicodeUtils;

import static com.nulldev.util.simplejava.SimpleJava.*;

import com.nulldev.util.crypto.api2.EncryptionUtil;
import com.nulldev.util.crypto.api2.EncryptionUtil.Types;
import com.nulldev.util.math.lNumber;

public class P2PEC_Crack_Test {

	public static void main(String[] args) {
		final String input = "Don't use this, trust me.";
		final String password = StringsUtil.randomString(256);
		
		println("Password: " + password);
		println("Encrypting...");
		final String enc = EncryptionUtil.byType(Types.P2PE_COMPRESS).encrypt(input, password);
		println("Encryption result: " + enc);
		
		println("Checking if we can normally decrypt it...");
		final String dec = EncryptionUtil.byType(Types.P2PE_COMPRESS).decrypt(enc, password);
		println("Regular decryption result: " + dec);
		println("P2PEC2 is working..." + (dec.equals(input) ? "YES" : "NO !!!!!!!!!"));
		if (!dec.equals(input)) {
			exit();
			return;
		}
		
		println("Decrypting via brute force + known password length...");
		lNumber attempt = lNumber.nil();
		while (true) {
			final String pword = StringsUtil.randomString(256);
			String out = EncryptionUtil.byType(Types.P2PE_COMPRESS).decrypt(enc, pword);
			if (UnicodeUtils.isSafe(out)) {
				if (out.equals(input)) {
					println("Data cracked, stop.");
					println("Data output: " + out);
					println("Password used: " + pword);
					println("Attempts: " + attempt);
					break;
				}else {
					attempt = attempt.add(1);
					//println("Attempting to crack data...Attempt #" + attempt);
				}
			}else {
				attempt = attempt.add(1);
				//println("Attempting to crack data...Attempt #" + attempt);
			}
		}
	}

}
